package com.chitry.jbrower.core;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.InputStream;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.chitry.jbrower.helper.ValidatorHelper;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

/**
 * @author Chitry
 * @date 2016年5月22日 下午6:58:07
 * @email chitry@126.com
 * @topic 浏览器核心承载类
 * @desc  传参方式：properties属性文件配置 [默认为百度首页]
 * @project JBrower
 * @version V1.0
 * @copyright Copyright ©2016 Chitry. All Rights Reserved.
 */
public class JBrowser extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JPanel webBrowserPanel;
	private JWebBrowser webBrowser;
	@SuppressWarnings("unused")
	private String url;

	public JBrowser(String url) {
		super(new BorderLayout());
		this.url = url;
		webBrowserPanel = new JPanel(new BorderLayout());
		webBrowser = new JWebBrowser();
		webBrowser.navigate(url);
		webBrowser.setButtonBarVisible(false);
		webBrowser.setMenuBarVisible(false);
		webBrowser.setBarsVisible(false);
		webBrowser.setStatusBarVisible(false);
		webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
		add(webBrowserPanel, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		String urlProp = "http://www.baidu.com";
		String titleProp = "百度一下，你就知道";
		String iconProp = "res/defaulticon.jpg";
		boolean isUndecoratedProp = false;
		boolean isVisibleProp = true;
		try {
			InputStream in = JBrowser.class.getClassLoader().getResourceAsStream("config.properties");
			Properties props = new Properties();
			props.load(in);
			urlProp = props.getProperty("s.url","http://www.baidu.com");
			urlProp = ValidatorHelper.IsUrl(urlProp)?urlProp:"http://www.baidu.com";//网址是否合法
			titleProp = props.getProperty("s.name","百度一下，你就知道");
			iconProp = props.getProperty("s.iconpath","/res/defaulticon.jpg");
			isUndecoratedProp = Boolean.parseBoolean(props.getProperty("f.isundecorated","false"));
			isVisibleProp = Boolean.parseBoolean(props.getProperty("f.isvisible","true"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		final String url = urlProp;
		final String title = titleProp;
		final int closeOperation = JFrame.EXIT_ON_CLOSE;
		final String borderLayout = BorderLayout.CENTER;
		final int extendState = JFrame.MAXIMIZED_BOTH;
		final boolean isLocationByPlatform = true;
		final boolean isUndecorated = isUndecoratedProp;
		final boolean isVisible = isVisibleProp;
		final Image icon = Toolkit.getDefaultToolkit().getImage(
				System.getProperty("user.dir") + iconProp);

		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame(title);
				frame.setDefaultCloseOperation(closeOperation);
				frame.getContentPane().add(new JBrowser(url),borderLayout);
				frame.setSize(1220, 680);
				frame.setExtendedState(extendState);
				frame.setLocationByPlatform(isLocationByPlatform);
				frame.setUndecorated(isUndecorated);
				frame.setIconImage(icon);
				frame.setLocationRelativeTo(null);// 窗体居中
//				frame.setResizable(false);
				frame.setVisible(isVisible);

			}
		});
		NativeInterface.runEventPump();
	}
}
